<?php
/**
 * 管理员控制器
 * User: 陈武
 * Date: 2018/5/14
 * Time: 8:56
 */

namespace Home\Controller;
use Home\Model\AuditModel;
use Home\Model\ConceptModel;
use Home\Model\QuestionModel;
use Home\Model\RelationModel;
use Home\Model\StockModel;
use Think\Controller;
class AdminController extends Controller
{
    /**
     * 众包审核
     */
    public function audit()
    {
        $this->display('audit');
    }

    /**
     * 股票列表
     */
    public function stock()
    {
        $this->display('stock');
    }

    /**
     * 概念列表
     */
    public function concept()
    {
        $this->display('concept');
    }

    /**
     * 概念股票列表
     */
    public function relation()
    {
        $stockModel = new StockModel();
        $conceptModel = new ConceptModel();
        $this->assign('stock', $stockModel->dic());
        $this->assign('concept', $conceptModel->dic());
        $this->display('relation');
    }

    /**
     * 用户列表
     */
    public function user()
    {
        $this->display('user');
    }

    /**
     * 获取众包审核列表
     */
    public function searchAudit()
    {
        $code = $_POST['code'];
        $name = $_POST['code_name'];
        $conceptName = $_POST['concept_name'];
        $page = $_POST['page'];
        $where = [];
        if ($code) {
            $where['code'] = $code;
        }
        if ($name) {
            $where['code_name'] = $name;
        }
        if ($conceptName) {
            $where['concept_name'] = $conceptName;
        }
        $model = new AuditModel();
        $data = $model->search($page, 10, $where);
        $total = $model->total($where);
        $result = [
            'page' => (int)$page,
            'size' => 10,
            'num' => ceil($total/10),
            'total' => $total,
            'data' => $data
        ];
        $this->ajaxReturn($result, 'json');
    }

    /**
     * 审核提交
     */
    public function apply()
    {
        $id = $_POST['id'];
        $status = $_POST['status'];
        $audit = new AuditModel();
        $res = $audit->apply($id, $status);
        if ($res) {
            $this->ajaxReturn(['code' => 0], 'json');
        } else {
            $this->ajaxReturn(['code' => -1], 'json');
        }
    }
    /**
     * 获取股票列表
     */
    public function searchStock()
    {
        $code = $_POST['code'];
        $name = $_POST['name'];
        $page = $_POST['page'];
        $where = [];
        if ($code) {
            $where['code'] = $code;
        }
        if ($name) {
            $where['name'] = $name;
        }
        $model = new StockModel();
        $data = $model->search($page, 10, $where);
        $total = $model->total($where);
        $result = [
            'page' => (int)$page,
            'size' => 10,
            'num' => ceil($total/10),
            'total' => $total,
            'data' => $data
        ];
        $this->ajaxReturn($result, 'json');
    }

    /**
     * 更新股票
     */
    public function saveStock()
    {
        $id = $_POST['id'];
        $data = [
            'name' => $_POST['name'],
            'org_name' => $_POST['org_name'],
            'address' => $_POST['address'],
            'main_business' => $_POST['main_business']
        ];
        $model = new StockModel();
        $res = $model->update(['id' => $id], $data);
        if ($res) {
            $this->ajaxReturn(['code' => 0], 'json');
        } else {
            $this->ajaxReturn(['code' => -1], 'json');
        }
    }

    /**
     * 获取概念列表
     */
    public function searchConcept()
    {
        $name = $_POST['name'];
        $page = $_POST['page'];
        $where = [];
        if ($name) {
            $where['name'] = $name;
        }
        $model = new ConceptModel();
        $data = $model->search($page, 10, $where);
        $total = $model->total($where);
        $result = [
            'page' => (int)$page,
            'size' => 10,
            'num' => ceil($total/10),
            'total' => $total,
            'data' => $data
        ];
        $this->ajaxReturn($result, 'json');
    }

    /**
     * 更新概念
     */
    public function saveConcept()
    {
        $id = $_POST['id'];
        $data = [
            'name' => $_POST['name'],
            'explain' => $_POST['explain'],
            'leading' => $_POST['leading']
        ];
        $model = new ConceptModel();
        if ($id) {
            $res = $model->update(['id' => $id], $data);
            if ($res) {
                $this->ajaxReturn(['code' => 0], 'json');
            } else {
                $this->ajaxReturn(['code' => -1], 'json');
            }
        } else {
            $res = $model->insert($data);
            if ($res) {
                $this->ajaxReturn(['code' => 0], 'json');
            } else {
                $this->ajaxReturn(['code' => -1], 'json');
            }
        }
    }

    /**
     * 删除概念
     */
    public function delConcept()
    {
        $id = $_POST['id'];
        $model = new ConceptModel();
        $res = $model->del(['id' => $id]);
        if ($res) {
            $this->ajaxReturn(['code' => 0], 'json');
        } else {
            $this->ajaxReturn(['code' => -1], 'json');
        }
    }

    /**
     * 获取概念股票列表
     */
    public function searchRelation()
    {
        $code = $_POST['code'];
        $codeName = $_POST['code_name'];
        $conceptName = $_POST['concept_name'];
        $page = $_POST['page'];
        $where = [];
        if ($code) {
            $where['code'] = $code;
        }
        if ($codeName) {
            $where['code_name'] = $codeName;
        }
        if ($conceptName) {
            $where['concept_name'] = $conceptName;
        }
        $model = new RelationModel();
        $data = $model->search($page, 10, $where);
        $total = $model->total($where);
        $result = [
            'page' => (int)$page,
            'size' => 10,
            'num' => ceil($total/10),
            'total' => $total,
            'data' => $data
        ];
        $this->ajaxReturn($result, 'json');
    }

    /**
     * 保存概念股票
     */
    public function saveRelation()
    {
        $data = [
            'save_concept' => $_POST['save_concept'],
            'save_code' => $_POST['save_code'],
            'explain' => $_POST['explain']
        ];
        $model = new RelationModel();
        $res = $model->add($data);
        if ($res) {
            $this->ajaxReturn(['code' => 0], 'json');
        } else {
            $this->ajaxReturn(['code' => -1], 'json');
        }
    }

    /**
     * 删除概念股票
     */
    public function delRelation()
    {
        $id = $_POST['id'];
        $model = new RelationModel();
        $res = $model->del(['id' => $id]);
        if ($res) {
            $this->ajaxReturn(['code' => 0], 'json');
        } else {
            $this->ajaxReturn(['code' => -1], 'json');
        }
    }
}